﻿using System.Text;
using PropertyExpression.Common;

namespace PropertyExpression.ControllableQuery.Test.Queries.StringBuilding
{
    public class ModelPublicationSearchQuery : IQuery<IModelPublicationInfo>
    {
        private readonly IOptionalValue<IParam<string>> publicationCode;
        private readonly IOptionalValue<IParam<string>> modelCode;
        private readonly IOptionalValue<IParam<string>> modelSequence;

        public ModelPublicationSearchQuery(
            IOptionalValue<IParam<string>> publicationCode,
            IOptionalValue<IParam<string>> modelCode,
            IOptionalValue<IParam<string>> modelSequence)
        {
            this.publicationCode = publicationCode;
            this.modelCode = modelCode;
            this.modelSequence = modelSequence;
        }

        public string TransformText()
        {
            var builder = new StringBuilder(
@"SELECT
    ModelPublication.PublicationId,
    ModelPublication.ModelCode,
    ModelPublication.ModelSequence
FROM
    ModelPublication");
            publicationCode.ProcessValue(value => builder.Append(@"
    INNER JOIN Publication
        ON ModelPublication.PublicationId = Publication.PublicationId"));
            builder.Append(@"
WHERE
    1 = 1");
            modelCode.ProcessValue(value => builder.Append(@"
    AND ModelPublication.ModelCode = " + value));
            modelSequence.ProcessValue(value => builder.Append(@"
    AND ModelPublication.ModelSequence = " + value));
            publicationCode.ProcessValue(value => builder.Append(@"
    AND Publication.Code = " + value));
            return builder.ToString();
        }
    }
}
